﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using SmartLib;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         lbEmail.Text = "";
        lbPassword.Text = "";
        if (!IsPostBack)
        {
            if (Request.Cookies["Username"] != null && Request.Cookies["Password"] != null)
            {
                txtusername.Text = Request.Cookies["Username"].Value.ToString();
                txtPassword.Text = Request.Cookies["Password"].Value.ToString();
            }
        }
    }
    void SignIn(String UserName, String Password)
    {
        
        String admin = "SELECT * FROM Admin1 WHERE UserName = @UserName AND Password = @Password AND IsAdmin=@IsAdmin";
        DataTable Admin = Database.GetData(admin, "@UserName", UserName, "@Password", Password, "@IsAdmin", "True");
        DataTable myData = Helper.GetDataTable("Select * from Admin1");
        if (Admin.Rows.Count != 0)
        {
            Session["Admin"] = Admin.Rows[0]["Username"];
            Response.Redirect("index.aspx");
        }
        else
        {
            String employ = "SELECT * FROM Admin1 WHERE UserName = @UserName AND Password = @Password AND IsAdmin=@IsAdmin";
            DataTable Employee = Database.GetData(employ, "@UserName", UserName, "@Password", Password, "@IsAdmin", "False");
            
            if (Employee.Rows.Count != 0)
            {
                Session["Employee"] = Employee.Rows[0]["Username"];
                Response.Redirect("index.aspx");
            }
            else
            {
                lblerror.Text = "Wrong User Name or Password ! Please check again ";
            }
           
        }
    }

    protected void btLogin_Click(object sender, EventArgs e)
    {
       this.SignIn(txtusername.Text, txtPassword.Text);
       // txtusername.Text = "";
        //txtPassword.Text = "";
        
        //ASP.quantringuoidung_login_aspx admin = (ASP.quantringuoidung_login_aspx)this.Master;
        
        //admin.refeshweb("SignIn.aspx");*/
    /*    bool check = false;
        SqlConnection con = new SqlConnection(new Connection().ConnectionString);
        con.Open();
        DataTable dt = new admin().LoadUser();

        foreach (DataRow row in dt.Rows)
        {
            if (row[3].ToString().Trim() == txtusername.Text.Trim())
            {
                if (row[2].ToString().Trim() == txtPassword.Text.Trim())
                {
                    Session["Name"] = row[3].ToString();
                    Session["ID"] = row[0].ToString();

                    if (cbRemember.Checked)
                    {
                        Response.Cookies["Email"].Value = txtEmail.Text;
                        Response.Cookies["Password"].Value = txtPassword.Text;
                    }
                    Response.Redirect("../index.aspx");
                    break;
                }
                else
                {
                    lbPassword.Text = "Invalid Password";
                    check = false;
                    break;
                }
            }
            else check = true;
            if (check)
            {
                lbEmail.Text = "Invalid Email";
            }
        }*/
    }
}